import {PassportStrategy} from "@nestjs/passport";
import { ExtractJwt, Strategy } from 'passport-jwt';
import {Injectable} from "@nestjs/common";
import {JwtConstant} from "../../common/constant/JwtConstant";

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor() {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            ignoreExpiration: false,
            secretOrKey: JwtConstant.secretKey,
        });
    }

    /**
     *
     * @param payload
     */
    async validate(payload: any) {
        console.log('JwtStrategy被守卫调用...');
        return {
            userId: payload.sub,
            username: payload.username,
            name: payload.name,
            roles: payload.roles
        };
    }

}
